<template>
  <div class="table-page">
    <a-form label-align="right" auto-label-width :model="form" class="form">
      <a-row :gutter="16" wrap>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="mobile" label="用户手机号">
            <a-input v-model="form.mobile" placeholder="请输入手机号码" />
          </a-form-item>
        </a-col>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="nickname" label="用户昵称">
            <a-input v-model="form.nickname" placeholder="请输入姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-space>
            <a-button type="primary" @click="getTableData">
              <template #icon>
                <icon-search />
              </template>
              <template #default>查询</template>
            </a-button>
            <a-button @click="handleReset">
              <template #default>重置</template>
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-form>

    <section class="table-box">
      <a-table
        row-key="id"
        size="small"
        page-position="bl"
        :bordered="{ cell: true }"
        :loading="loading"
        :data="tableData"
        :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
        :pagination="{ showPageSize: true, showTotal: true, total: total, current: current, pageSize: pageSize }"
        @page-change="changeCurrent"
        @page-size-change="changePageSize"
      >
        <template #columns>
          <a-table-column fixed="left" title="用户ID" :width="80" data-index="id"></a-table-column>
          <a-table-column fixed="left" title="用户昵称" data-index="nickname" :width="120"></a-table-column>
          <a-table-column title="头像" :width="80">
            <template #cell="{ record }">
              <a-avatar>
                <img v-if="record.headimage && record.headimage !== ' '" :src="record.headimage" />
                <img v-else alt="avatar" src="@/assets/images/img／defaultAvatar@2x.png" />
              </a-avatar>
            </template>
          </a-table-column>
          <a-table-column title="用户手机号" data-index="mobile" :width="150"></a-table-column>
          <a-table-column title="注册时间" :width="180" data-index="create_time" ellipsis tooltip></a-table-column>
          <a-table-column title="用户状态" :width="100" align="center">
            <template #cell="{ record }">
              <a-typography-text v-if="record.status == 1" type="success">正常</a-typography-text>
              <a-typography-text v-else type="danger">禁用</a-typography-text>
            </template>
          </a-table-column>
          <a-table-column title="账户余额" :width="140" data-index="balance"></a-table-column>
          <a-table-column title="备注" :min-width="200" data-index="remarks"></a-table-column>

          <a-table-column fixed="right" title="操作" :width="400" align="center">
            <template #cell="{ record }">
              <a-space :size="4">
                <a-link @click="onDetail(record)">
                  <template #default>查询消费明细</template>
                </a-link>
                <a-link @click="handleEdit(record)" status="warning">
                  <template #default>修改用户</template>
                </a-link>
                <a-link @click="handleBalance(record)" status="warning">
                  <template #default>充值余额</template>
                </a-link>
                <a-popconfirm
                  v-if="record.status == 1"
                  @ok="handleStop(record)"
                  type="warning"
                  content="您确定要禁用吗?"
                >
                  <a-link status="danger">
                    <template #default>禁用</template>
                  </a-link>
                </a-popconfirm>
                <a-popconfirm v-else @ok="handleStart(record)" type="warning" content="您确定要启用吗?">
                  <a-link>
                    <template #default>启用</template>
                  </a-link>
                </a-popconfirm>
              </a-space>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </section>

    <EditModal @reFreshTable="getTableData" ref="EditModalRef"></EditModal>
    <balanceModal @reFreshTable="getTableData" ref="balanceModalRef"></balanceModal>
  </div>
</template>

<script setup lang="ts" name="MainTable">
import { reactive, ref } from 'vue'
import { usePagination } from '@/hooks'
import { getCustomerList, uploadUserStart, uploadUserStop } from '@/apis'
import { useRouter } from 'vue-router'
import EditModal from './EditModal.vue'
import balanceModal from './balanceModal.vue'
import { Message } from '@arco-design/web-vue'

const router = useRouter()
const form = reactive({
  mobile: '',
  nickname: ''
})

const loading = ref(false)
const tableData = ref([])

const { current, pageSize, total, changeCurrent, changePageSize, setTotal } = usePagination(() => getTableData())

const handleStop = async (item) => {
  const res = await uploadUserStop({ customer_id: item.id })
  const resData = JSON.parse(res)
  if (resData.code === 200) {
    Message.success(resData.message)
    getTableData()
  } else {
    Message.error(resData.message)
  }
}
const handleStart = async (item) => {
  const res = await uploadUserStart({ customer_id: item.id })
  const resData = JSON.parse(res)
  if (resData.code === 200) {
    Message.success(resData.message)
    getTableData()
  } else {
    Message.error(resData.message)
  }
}
const getTableData = async () => {
  loading.value = true
  const res = await getCustomerList({
    page: current.value,
    limit: pageSize.value,
    ...form
  })
  let resData = JSON.parse(res)

  if (resData.code === 200) {
    tableData.value = resData.result.data
    setTotal(resData.result.total)
    loading.value = false
  } else {
    loading.value = false
  }
}
const handleReset = () => {
  form.mobile = ''
  form.nickname = ''
  getTableData()
}
const EditModalRef = ref<InstanceType<typeof EditModal>>()

const handleEdit = (item) => {
  EditModalRef.value?.edit(item.id)
}
const balanceModalRef = ref()
const handleBalance = (item) => {
  balanceModalRef.value?.edit(item.id)
}

const onDetail = (item) => {
  router.push({
    path: '/customer-list/detail',
    query: { id: item.id, name: item.nickname, mobile: item.mobile, headimage: item.headimage }
  })
}

getTableData()
</script>

<style lang="scss" scoped>
:deep(.arco-grid) {
  flex-wrap: wrap;
}
:deep(.arco-grid-item) {
  min-width: 250px;
}
:deep(.arco-alert-success) {
  padding: 14px 16px;
  border-color: rgb(var(--success-6));
  background-color: rgba(var(--success-6), 0.08);
  .arco-alert-content {
    color: rgb(var(--success-6));
    font-size: 12px;
  }
}
.table-page {
  height: 100%;
  overflow: hidden;
  margin: $margin;
  background: var(--color-bg-1);
  padding: $padding;
  padding-bottom: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .form {
    margin-top: 12px;
    :deep(.arco-form-item) {
      margin-bottom: 10px;
    }
  }
  .table-box {
    flex: 1;
    overflow: hidden;
    margin-top: 12px;
    padding-bottom: 16px;
  }
}
</style>
